#include"io430.h"
#include"intrinsics.h"

void Init_flash(void)
{
  FCTL2=FWKEY+0x53; 
};

void read_flash(unsigned char* source,unsigned char* dist,unsigned char _num)
{
  unsigned char _i;
  
   for(_i=0;_i<_num;_i++)
   {
      dist[_i]=source[_i];
   };
};
unsigned char _rrr;

void write_flash(unsigned char _offset_Flash_ptr,unsigned char* source,char num,char* tmp_buffer)
{
  char *Flash_ptr;                          // Flash pointer
  unsigned char _i;
    
  P1OUT|=BIT2;
  
  read_flash((unsigned char*)0x1000,(unsigned char*)tmp_buffer,10);  
  
  Flash_ptr = (char*)0x1000;                // Initialize Flash pointer
  FCTL3 = FWKEY;                            // Clear Lock bit
  FCTL1 = FWKEY + ERASE;                    // Set Erase bit  
  *Flash_ptr = 0;                           // Dummy write to erase Flash seg
    
  for (_i = _offset_Flash_ptr; _i < num+_offset_Flash_ptr; _i++)
  {
    tmp_buffer[_i]=source[_i-_offset_Flash_ptr];                   // Write value to flash
  }
    
  FCTL1 = FWKEY + WRT;                      // Set WRT bit for write operation
  
  for (_i = 0; _i < 10; _i++)
  {
    *Flash_ptr++ =tmp_buffer[_i];                   // Write value to flash
  }

  FCTL1 = FWKEY;                            // Clear WRT bit
  FCTL3 = FWKEY + LOCK;                     // Set LOCK bit
  
  P1OUT&=~BIT2;
}

